home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / langguid / chap_07 / search.sx
Encoding:
Text File  |  1996-05-21  |  5.3 KB  |  186 lines  |  [TEXT/ttxt]

  1. --<<<
  2. -- Kaleida Labs, Inc.
  3. -- Field Guide to the ScriptX Language
  4. -- chapter 7, example 1
  5.  
  6. -- create a string constant
  7. global str := "This is a sample string."
  8.  
  9. -- search for the fourth word in str, starting at the cursor position 
  10. -- 0 and continuing until the end (str.size)
  11. global args := #(str, 4, 0, str.size) as Quad
  12. findNthContext args @word
  13.  
  14. -- print out the word returned
  15. copyFromTo args[1] args[3] args[4]
  16.  
  17. -- search for the second set of characters bounded by ":"
  18. global s := "first name:last name:street address:city:state"
  19. global args := #(s, 2, 0, s.size) as Quad
  20. findNthContext args (r -> r == ":"[1])
  21.  
  22. -- print out the characters returned
  23. copyFromTo args[1] args[3] args[4]
  24.  
  25. -->>>
  26.  
  27.  
  28.  
  29.  
  30. --<<<
  31. -- Kaleida Labs, Inc.
  32. -- Field Guide to the ScriptX Language
  33. -- chapter 7, example 2
  34.  
  35. -- create a string
  36. global myString := "Newton devised a new system."
  37.  
  38. -- create a StringIndex object 
  39. global strIndex := new StringIndex string:myString
  40.  
  41. -- create a SearchContext object suitable for beginning the search at 
  42. -- the beginning of the string
  43. global sc := initialSearchContext strIndex 0
  44.  
  45. -- search for the first occurrence of "new" as a whole word
  46. sc := searchIndex strIndex "new" sc true
  47.  
  48. -- print out the result
  49. copyFromTo sc.string sc.startOffset sc.endOffset
  50.  
  51.  
  52. -- search for the first occurrence of "new" as a whole word or as part 
  53. -- of a larger word.  We need a new "sc" to start at the beginning of the string.
  54. sc := initialSearchContext strIndex 0
  55. searchIndex strIndex "new" sc false
  56.  
  57. -- print out the result (Note that searchIndex is not case sensitive.)
  58. copyFromTo sc.string sc.startOffset sc.endOffset
  59.  
  60.  
  61. -->>>
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. -- create a string
  78. global str := “This is a sample string.”
  79.  
  80. -- search for the fourth word in str, starting at the cursor position -- 0 and continuing until the end (str.size)
  81. global args := #(str, 4, 0, str.size) as Quad
  82. findNthContext args @word
  83.  
  84. -- print out the word returned
  85. copyFromTo args[1] args[3] args[4]
  86.  
  87.  
  88. -- search for the second set of characters bounded by ":"
  89. s := "first name:last name:street address:city:state"
  90. args := #(s, 2, 0, size.s) as Quad
  91. findNthContext args (r -> r == ":"[1])
  92.  
  93. -- print out the characters returned
  94. copyFromTo args[1] args[3] args[4]
  95.  
  96.  
  97.  
  98.  
  99. A second global function for searching strings is searchIndex, which finds the first occurrence of the string you want to match. (Note that the string you want to match must be at least three characters long and cannot contain any spaces.) Searching is fast because searchIndex is actually searaching a signature index, which is built automatically when you create a StringIndex object and supply a string for its string instance variable.
  100. searchIndex strIndex  match  searchContext  wholeWord
  101. This function searches the StringIndex object strIndex for the first occurrence of match using searchContext to tell it where to search. The last argument, wholeWord, is either true or false, indicating whether the match must be a whole word (as opposed to only part of a word).
  102. The following code example demonstrates using searchIndex. It will be easier to follow if you have read the entries for SearchContext and StringIndex in ScriptX Class Reference.
  103.  
  104. -- create a string
  105. global myString := "Newton devised a new system."
  106.  
  107. -- create a StringIndex object 
  108. global strIndex := new StringIndex string:myString
  109.  
  110. -- create a SearchContext object suitable for beginning the search at -- the beginning of the string
  111. global sc := initialSearchContext strIndex 1
  112.  
  113. -- search for the first occurrence of "new" as a whole word
  114. searchIndex strIndex "new" sc true
  115.  
  116. -- print out the result
  117. copyFromTo sc.string sc.startOffset sc.endOffset
  118. "new"
  119.  
  120. -- search for the first occurrence of "new" as a whole word or as part -- of a larger word
  121. searchIndex strIndex "new" sc false
  122.  
  123. -- print out the result (Note that searchIndex is not case sensitive.)
  124. copyFromTo sc.string sc.startOffset sc.endOffset
  125. "Newton"
  126.  
  127.  
  128.  
  129.  
  130.  
  131. global myString := "Able was I ere I saw Elba" as String 
  132.  
  133. select first word where it[1] = "s"[1] in myString 
  134. select first word where (getFirst it) = "s"[1] in myString
  135. select first word where (findRange it "s") = 1 in myString
  136.  
  137. global str := "hello there"
  138. contexts str
  139. str := undefined 
  140.  
  141. global myList := new LinkedList
  142. contexts myList
  143.  
  144. -- Selecting Named Objects
  145.  
  146. class MyClass () end -- a parent class you want to add a name to
  147.  
  148. class NamedClass (MyClass)
  149.     instance vars name 
  150. end
  151.  
  152. -- a heterogeneous collection of objects, all inheriting from NamedClass
  153. myArray := #(
  154.     (object (Region, NamedClass) settings name:"Phil" end),
  155.     (object (Rect, NamedClass) settings name:"Anastasia" end),
  156.     (object (Oval, NamedClass) settings name:"Steve" end),
  157.     (object (Bitmap, NamedClass) bbox:(new Rect x2:100 y2:100) \
  158.             settings name:"Marcia" end)
  159. )
  160.  
  161. -- some test path expressions
  162. select thing named "Marcia" in myArray
  163. select thing named "Steve" in myArray
  164.  
  165. -- Path Expressions based on Context
  166.  
  167. global str := "This is a sentence. This is another sentence. Wow."
  168. global arr := #("first string", 24, (object (Rect) end), "2nd string",
  169.     345, 6321, "3rd and last string")
  170.  
  171. -- some test path expressions
  172. select word 1 in str
  173. select character 29 in str
  174. select sentence 2 in str
  175. select Rect 1 in arr
  176. select String 3 in arr
  177. select String 4 in arr
  178. select thing 2 in arr
  179.  
  180. -- now set globals to undefined to dispose of them
  181. str := undefined
  182. myList := undefined
  183. myArray := undefined
  184. arr := undefined
  185. -->>>
  186.